#include "stdafx.h"
#include "GameMap.h"
#include <time.h>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
GameMap::GameMap(void)
{
}
GameMap::GameMap(char* file)
{
		textureNum = loadGameMap(file);
		setupVertices();
		setupNormals();
		setupIndices();
}

void GameMap::draw(){
	glPushMatrix();

	glEnable(GL_COLOR_MATERIAL);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3,GL_INT,0,vertexArray);
	glEnableClientState(GL_COLOR_ARRAY);
	glColorPointer(3,GL_UNSIGNED_BYTE,0,colorArray);
	glEnableClientState(GL_NORMAL_ARRAY);
	glNormalPointer(GL_FLOAT,0,normalArray);
	
	if (indexCount != 0x00000000){
		
		glDrawElements(GL_TRIANGLE_STRIP, *indexCount, GL_UNSIGNED_INT, indexArray);
	}
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	
	glDisable(GL_COLOR_MATERIAL);
	glPopMatrix();

}
void GameMap::setupVertices(){
	//textureNum is the identifier generated by glGenTextures

	//Bind the texture again, and extract the needed data
	glBindTexture(GL_TEXTURE_2D, textureNum);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
	GLint i = height*width;
	GLubyte * imageData = new GLubyte[i+1];
	glGetTexImage(GL_TEXTURE_2D,0,GL_RED, GL_UNSIGNED_BYTE, &imageData[0]);

	//Setup varibles: counter (used for counting vertices)
	//VertexArray: pointer to address for storing the vertices. Size: 3 ints per point, width*height points total
	//ColorArray: pointer to address for storing the color data. 3 bytes per point.
	int counter = 0;
	vertexArray = new GLint[height*width*3*resolution*resolution];
	colorArray = new GLubyte[height*width*3*resolution*resolution];
		
	srand(time(NULL));
	//Loop through rows
	for (int z = 0; z < height*resolution; z++){
		//Loop along the line
		for (int x=0; x < width*resolution; x++){
			//Add vertices: x, y, redChannel
			//Add colordata: the common-color.
			colorArray[counter] = 40;
			vertexArray[counter++] = x;

			int h = 0;
			if ((x/2 % 2 == 0) && (z/2 % 2 == 0)){
				h = imageData[x/2+(z/2)*width];
			}
			else if (x/2 % 2 == 0){
				int h1 = imageData[x/2+((z+1)/2)*width];
				int h2 = imageData[x/2+((z-1)/2)*width];
				h = (h1+h2)/2;
			}
			else if (z/2 % 2 == 0){
				int h1 = imageData[(x+1)/2+(z/2)*width];
				int h2 = imageData[(x-1)/2+(z/2)*width];
				h = (h1+h2)/2;
			}
			else{
				int h1 = imageData[(x+1)/2+((z+1)/2)*width];
				int h2 = imageData[(x-1)/2+((z-1)/2)*width];
				int h3 = imageData[(x+1)/2+((z-1)/2)*width];
				int h4 = imageData[(x-1)/2+((z+1)/2)*width];
				h = (h1+h2+h3+h4)/4;
			}

			
			colorArray[counter] =120;
			vertexArray[counter++] = (h * 20) / 255;


			colorArray[counter] =  40;
			vertexArray[counter++] = z;

		}
	}
	//"Return" total vertice amount
	vertexCount = new GLsizei(counter);
	
}
void GameMap::setupNormals(){
	normalArray = new GLfloat[height*resolution*width*resolution*3*2];
	int counter = 0;
	//Loop through rows
	for (int y = 0; y < (height*resolution)-1; y++){
		if (y % 2 == 0){
		//Loop along the line
			for (int x=0; x < (width*resolution)-1; x++){
				//Even row, go forwards
				/* Legend:
				5 -- 6 -- 7
				| /  |  / |
				3 -- C -- 4
				|  \ |  \ |
				0 -- 1 -- 2
				*/
				//Indices for points
				int i[8];
				int rowY =  y * width * resolution;
				int rowX = x * resolution;
				int c = rowY + rowX;
					
				i[0] = rowX - 1 + rowY - width * resolution;
				i[1] = rowX		+ rowY - width * resolution;
				i[2] = rowX	+ 1 + rowY - width * resolution;

				i[3] = rowX - 1 + rowY;
				i[4] = rowX + 1 + rowY;

				i[5] = rowX - 1 + rowY + width * resolution;
				i[6] = rowX		+ rowY + width * resolution;
				i[7] = rowX + 1 + rowY + width * resolution;

				GLfloat normals[24];
				//Most cases
				if (x > 0 && y > 0 && x < (width * resolution) && y < (height & resolution) ){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[12]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[18]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 4; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 4;
					y /= 4;
					z /= 4;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Lower left corner
				else if (y == 0 && x == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[18]);
					GLfloat x, y,z;
					x = (normals[18] + normals[21])/2;
					y = (normals[19] + normals[22])/2;
					z = (normals[20] + normals[23])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Lower right corner
				else if (y == 0 && x == (width *resolution)){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[12]);
					GLfloat x, y,z;
					x = (normals[12] + normals[15])/2;
					y = (normals[13] + normals[16])/2;
					z = (normals[14] + normals[17])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Upper left corner
				else if (y > 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					GLfloat x, y,z;
					x = (normals[6] + normals[9])/2;
					y = (normals[7] + normals[10])/2;
					z = (normals[8] + normals[11])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Upper right corner
				else if (y > 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					GLfloat x, y,z;
					x = (normals[0] + normals[3])/2;
					y = (normals[1] + normals[4])/2;
					z = (normals[2] + normals[5])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Bottom row
				else if (y == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Top row
				else if (y == height*resolution){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Left row
				else if (x == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Right row
				else if (y == height*resolution){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
			}
		}
		else{
		//Loop along the line
			for (int x=(width*resolution)-1; x > 0; x--){
				//Even row, go forwards
				/* Legend:
				5 -- 6 -- 7
				| /  |  / |
				3 -- C -- 4
				|  \ |  \ |
				0 -- 1 -- 2
				*/
				//Indices for points
				int i[8];
				int rowY =  y * width * resolution;
				int rowX = x * resolution;
				int c = rowY + rowX;
					
				i[0] = rowX - 1 + rowY - width * resolution;
				i[1] = rowX		+ rowY - width * resolution;
				i[2] = rowX	+ 1 + rowY - width * resolution;

				i[3] = rowX - 1 + rowY;
				i[4] = rowX + 1 + rowY;

				i[5] = rowX - 1 + rowY + width * resolution;
				i[6] = rowX		+ rowY + width * resolution;
				i[7] = rowX + 1 + rowY + width * resolution;

				GLfloat normals[24];
				//Most cases
				if (x > 0 && y > 0 && x < (width * resolution) && y < (height & resolution) ){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[12]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[18]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 4; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 4;
					y /= 4;
					z /= 4;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Lower left corner
				else if (y == 0 && x == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[18]);
					GLfloat x, y,z;
					x = (normals[18] + normals[21])/2;
					y = (normals[19] + normals[22])/2;
					z = (normals[20] + normals[23])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Lower right corner
				else if (y == 0 && x == (width *resolution)){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[12]);
					GLfloat x, y,z;
					x = (normals[12] + normals[15])/2;
					y = (normals[13] + normals[16])/2;
					z = (normals[141] + normals[17])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Upper left corner
				else if (y > 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					GLfloat x, y,z;
					x = (normals[6] + normals[9])/2;
					y = (normals[7] + normals[10])/2;
					z = (normals[8] + normals[11])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Upper right corner
				else if (y > 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					GLfloat x, y,z;
					x = (normals[0] + normals[3])/2;
					y = (normals[1] + normals[4])/2;
					z = (normals[2] + normals[5])/2;
					float mag = sqrt(x * x + y * y + z * z);
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Bottom row
				else if (y == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Top row
				else if (y == height*resolution){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Left row
				else if (x == 0){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[4]], &vertexArray[i[2]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[4]], &vertexArray[i[6]], &vertexArray[i[7]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
				//Right row
				else if (y == height*resolution){
					doTriangleNormal(&vertexArray[c], &vertexArray[i[1]], &vertexArray[i[3]], &vertexArray[i[0]], &normals[0]);
					doTriangleNormal(&vertexArray[c], &vertexArray[i[3]], &vertexArray[i[6]], &vertexArray[i[5]], &normals[6]);
					GLfloat x, y,z;
					x = y = z = 0;
					for (int i = 0; i < 2; i++){
						x += normals[i*6] + normals[(i+3)*6];
						y += normals[i*6+1] + normals[(i+3)*6+1];
						z += normals[i*6+2] + normals[(i+3)*6+2];
					}
					float mag = sqrt(x * x + y * y + z * z);
					x /= 2;
					y /= 2;
					z /= 2;
					x /= mag;
					y /= mag;
					z /= mag;
					normalArray[counter++] = x;
					normalArray[counter++] = y;
					normalArray[counter++] = z;
				}
			}
		}
	}
}


void GameMap::doTriangleNormal(GLint* p1, GLint* p2, GLint* p3, GLint* p4, GLfloat* n){
	
		
			GLfloat vectorU[3] = {
								(GLfloat)p2[0]	-	p1[0],
								(GLfloat)p2[1] - p1[1],
								(GLfloat)p2[2] - p1[2]
			};
			
			GLfloat vectorV[3] = {
								(GLfloat)p3[0]	-	p1[0],
								(GLfloat)p3[1]	-	p1[1],
								(GLfloat)p3[2]	-	p1[2]
			};

			GLfloat normalVector[3] = {
								(GLfloat)	vectorU[1]	*	vectorV[2]	-	vectorU[2]	*	vectorV[1],
								(GLfloat)	vectorU[2]	*	vectorV[0]	-	vectorU[0]	*	vectorV[2],
								(GLfloat)	vectorU[0]	*	vectorV[1]	-	vectorU[1]	*	vectorV[0]
			};


			n[0] = normalVector[0];
			n[1] = normalVector[1];
			n[2] = normalVector[2];

			float mag = sqrt(n[0] * n[0] +  n[1] * n[1] + n[2] * n[2]);

			n[0] = n[0]/mag;
			n[1] = n[1]/mag;
			n[2] = n[2]/mag;
			//Triangle 2
			vectorU[0] = (GLfloat)p2[0]	-	p4[0];
			vectorU[1] = (GLfloat)p2[1]	-	p4[1];
			vectorU[2] = (GLfloat)p2[2]	-	p4[2];
				
			vectorV[0] = (GLfloat)p3[0]	-	p4[0];
			vectorV[1] = (GLfloat)p2[1]	-	p4[1];
			vectorV[2] = (GLfloat)p3[2]	-	p4[2];

			normalVector[0] = vectorU[1] *vectorV[2] - vectorU[2] * vectorV[1];
			normalVector[1] = vectorU[2] *vectorV[0] - vectorU[0] * vectorV[2];
			normalVector[2] = vectorU[0] *vectorV[1] - vectorU[1] * vectorV[0];
			n[3] = normalVector[0];
			n[4] = normalVector[1];
			n[5] = normalVector[2];

			
			mag = sqrt(n[3] * n[3] +  n[4] * n[4] + n[5] * n[5]);

			n[3] = n[3]/mag;
			n[4] = n[4]/mag;
			n[5] = n[5]/mag;
		}

int GameMap::getHeightAt(int x, int z){
			return vertexArray[(x+z*(width*resolution))*3+1];
	}
void GameMap::setupIndices(){
	//Pointer to location for storing indices. Size: 2 triangles per square, 3 points per triangle, width*height triangles
	indexArray = new GLuint[width*resolution*height*resolution*2*3];
	int counter = 0;
	//Loop through rows, don't go to top row (because those triangles are to the row below)
	for (int y = 0; y < (height*resolution)-1; y++){
		//Loop along the line, don't go to last point (those are connected to second last point)
		if (y % 2 == 0){
			for (int x=0; x < (width*resolution); x++){
				//
				//	TL___TR
				//	|  /  |
				//	LL___LR
					int lowerLeft = x + (width*resolution) * y;
					int lowerRight = (x + 1) + (width*resolution) * y;
					int topLeft = x + (width*resolution) * (y + 1);
					int topRight = (x + 1) + (width*resolution) * (y + 1);
 
					indexArray[counter++] = lowerLeft;
					//indexArray[counter++] = lowerRight;
					indexArray[counter++] = topLeft;

				    //indexArray[counter++] = topLeft;
					//indexArray[counter++] = lowerRight;
					//indexArray[counter++] = topRight;
			}
		}
		else{
			for (int x=(width*resolution)-1; x > 0; x--){
				//
				//	TL___TR
				//	|  /  |
				//	LL___LR
				int lowerRight =	x + (width*resolution) * y;
				int topRight =		x + (width*resolution) * (y + 1);
 
					
				//indexArray[counter++] = lowerRight;
				//indexArray[counter++] = topLeft;

				//indexArray[counter++] = topLeft;
				indexArray[counter++] = lowerRight;
				indexArray[counter++] = topRight;
			}
		}
		indexArray[counter++]=indexArray[counter-1];
	}
	//"Return" the amount of indices
	indexCount = new GLsizei(counter);
}

GameMap::~GameMap(void)
{
}

GLuint GameMap::loadGameMap(char *  path){
	
	
	
	unsigned char header[54]; 
	unsigned int dataPos;     
	unsigned int width, height;
	unsigned int imageSize;   
	unsigned char * data;

	FILE * file;
	int l = fopen_s(&file,path, "rb");
	if (!file)                              {
		printf("Image could not be opened\n"); 
		return 0;
	}
		if ( fread(header, 1, 54, file)!=54 ){
		printf("Not a correct BMP file\n");
		return false;
	}
		if ( header[0]!='B' || header[1]!='M' ){
		printf("Not a correct BMP file\n");
		return 0;
	}
	
		// Read ints from the byte array
	dataPos    = *(int*)&(header[0x0A]);
	imageSize  = *(int*)&(header[0x22]);
	width      = *(int*)&(header[0x12]);
	height     = *(int*)&(header[0x16]);

	// Some BMP files are misformatted, guess missing information
	if (imageSize==0)    imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
	if (dataPos==0)      dataPos=54; // The BMP header is done that way

	// Create a buffer
	data = new unsigned char [imageSize*3];
 
	// Read the actual data from the file into the buffer
	fread(data,1,imageSize,file);
 
	//Everything is in memory now, the file can be closed
	fclose(file);
	// Create one OpenGL texture
	GLuint textureID;
	glGenTextures(1, &textureID);
 
	// "Bind" the newly created texture : all future texture functions will modify this texture
	glBindTexture(GL_TEXTURE_2D, textureID);
 
	// Give the image to OpenGL
	glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	return textureID;
	
}